--
-- Author: shunguo.chen
-- Date: 2019-05-23 20:12:24
--
-- SkillModel 技能数据模块
--

local Skill = require("app.public.dao.Skill")
local SpecType = require("app.public.dao.SpecType")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self:clear()
end

function ClassRef:clear()
    self._speedRoleSkillDict = {}
    self._speedSpecType = nil
    self._speedReplaceSkills = nil -- 可替换的技能列表，顺序不变
    self._speedEquipedSkills = nil -- 已装备的技能列表，按index排序
end

function ClassRef:getSpeedSpecType()
    return self._speedSpecType
end
function ClassRef:getSpeedReplaceSkills()
    return self._speedReplaceSkills
end
function ClassRef:getSpeedEquipedSkills()
    return self._speedEquipedSkills
end

function ClassRef:_getSpeedRoleSkill(skillId)
    local skill = self._speedRoleSkillDict[skillId]
    if not skill then
        skill = Skill.new()
        skill:read({uid=skillId})
        skill:setLearned()
        self._speedRoleSkillDict[skillId] = skill
    end
    return skill
end

------------------------ receive ------------------------
-- message S2C_SpeedDungeonRoleSkills{
--     int32 RoleID = 1; //扮演角色ID
--     repeated RolePlaySkill skills = 2;
-- }
-- message RolePlaySkill{
--     int32 classSkillID = 1; //技能ID
--     int32 index = 2; //位置下标，没装备为-1
-- }
function ClassRef:onSpeedDungeonRoleSkills(msg)
    if not msg then return end

    local roleConf = GD:queryCosplayConf(msg.RoleID)
    if not roleConf then return end

    if not self._speedSpecType then
        self._speedSpecType = SpecType.new(tonumber(roleConf.specID))
        self._speedSpecType:setActive(true)
        self._speedSpecType:setRoleExtraParams(L(roleConf.dutyShow), L(roleConf.name))
    end

    self._speedReplaceSkills = {} -- 可替换的技能列表，顺序不变
    self._speedEquipedSkills = {} -- 已装备的技能列表，按index排序

    local normalSkillId = tonumber(roleConf.classSkill1)
    local skillId, skill
    for i,v in ipairs(msg.skills) do
        skillId = v.classSkillID
        skill = self:_getSpeedRoleSkill(skillId)
        if skillId ~= normalSkillId then
            --普攻不可替换
            table.insert(self._speedReplaceSkills, skill)
        end
        if v.index ~= -1 then
            --下标不等于-1的技能表示已装备
            self._speedEquipedSkills[v.index] = skill
        end
    end

    app:sendMsg("SkillChangeController", "showView")
end

------------------------ request ------------------------
-- 激活专精
function ClassRef:C2S_SetSkillChooseID(specId, isReset)
    app.session:request("action.C2S_SetSkillChooseID", {ID=specId, isReset=isReset}, function(msg)
        -- dump(msg, "C2S_SetSkillChooseID")
        UD:onActiveSpecType(msg.newTalents, msg.oldTalents)

        app:sendMsg("SkillController", "onChooseSpec")
        UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_SKILL, true)
    end)
end

-- 学习/升级技能
function ClassRef:C2S_StudySkill(skillId)
    app.session:request("action.C2S_StudySkill", {uid = skillId}, function(msg)
        -- dump(msg, "C2S_StudySkill")
        app:sendMsg("SkillController", "onSkillUpdate", UD:getSkill(skillId))
        UD.redPoint:checkStateByKey(RedPointConstant.Skill_Skill)
    end)
end

-- 选择技能
function ClassRef:C2S_SkillChoose(params)
    dump(params, "C2S_SkillChoose params")
    app.session:request("action.C2S_SkillChoose", params, function(msg)
        dump(msg, "C2S_SkillChoose msg")

        -- 如果是legendSkillTeamId，则需要换成skillTeamId
        local teamId = params.teamId
        local realTeamId = GD:getLegendSkillTeam(teamId)
        if realTeamId > 0 then
            teamId = realTeamId
        end
        print("C2S_SkillChoose", teamId, realTeamId)
        app:sendMsg("SkillController", "onChooseSkill", teamId)
        UD.redPoint:checkStateByKey(RedPointConstant.Skill_Skill)
    end)
end

-- 学习天赋
function ClassRef:C2S_StudyTalent(skillId)
    app.session:request("action.C2S_StudyTalent", {uid=skillId}, function(msg)
        -- dump(msg, "C2S_StudyTalent")
        app:sendMsg("SkillController", "onTalentUpdate", UD:getTalent(skillId))
        UD.redPoint:checkStateByKey(RedPointConstant.Skill_Talent)
    end)
end

-- 重置天赋
function ClassRef:C2S_ClearTalents()
    app.session:request("action.C2S_ClearTalents", {}, function(msg)
        app:sendMsg("SkillController", "onTalentReset")
    end)
end

-- 镶嵌符文
function ClassRef:C2S_PutRune(runeId)
    app.session:request("pb_rune.C2S_PutRune", {runeID=runeId},
        function(msg)
            if msg.ok then
                display.pushToast( L("lua_code_text_168") )
                app:sendMsg("SkillController", "onRuneUpdate", UD:getRune(runeId))
            end
        end,
        nil,
        function(errMsg)
            return false
        end,
        {block = true}
    )
end

-- 卸下符文
function ClassRef:C2S_RemoveRune(runeId)
    app.session:request("pb_rune.C2S_RemoveRune", {runeID=runeId},
        function(msg)
            if msg.ok then
                display.pushToast( L("lua_code_text_119") )
                app:sendMsg("SkillController", "onRuneUpdate", UD:getRune(runeId))
            end
        end,
        nil,
        function(errMsg)
            return false
        end,
        {block = true}
    )
end


-- 请求竞速扮演角色的技能信息
function ClassRef:C2S_SpeedDungeonRoleSkills()
    app.session:request("pb_speeddungeon.C2S_SpeedDungeonRoleSkills", {})
end

-- 切换竞速扮演角色的技能
function ClassRef:C2S_SpeedDungeonChooseSkill(params)
    app.session:request("pb_speeddungeon.C2S_SpeedDungeonChooseSkill", params)
end

return ClassRef
